/**
 * @author thaodo
 *
 *
 */
package com.mfsoft.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.example.mpsoft.RecyclingImageView;
import com.mfsoft.camera.R;
import com.mfsoft.model.ImageItem;
import com.mfsoft.utilities.Variables;

public class GridViewAdapter extends ArrayAdapter<ImageItem> {
	private Context context;
	// cai int nay la cai gi day
	private int layoutResourceId;
	private List<ImageItem> imageItems;
	private ViewAdapter viewAdapter;
	private AQuery aq;
	private Bitmap placehoder;
	private LayoutInflater inflater;
	private int imageWidth;

	public GridViewAdapter(Context context, int layoutResourceId,
			List<ImageItem> imageItems, int imageWidth) {
		super(context, layoutResourceId, imageItems);
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.imageItems = imageItems;
		this.imageWidth = imageWidth;
		inits();
	}

	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((context == null) ? 0 : context.hashCode());
		result = prime * result
				+ ((imageItems == null) ? 0 : imageItems.hashCode());
		result = prime * result + imageWidth;
		result = prime * result + layoutResourceId;
		return result;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GridViewAdapter other = (GridViewAdapter) obj;
		if (context == null) {
			if (other.context != null)
				return false;
		} else if (!context.equals(other.context))
			return false;
		if (imageItems == null) {
			if (other.imageItems != null)
				return false;
		} else if (!imageItems.equals(other.imageItems))
			return false;
		if (imageWidth != other.imageWidth)
			return false;
		if (layoutResourceId != other.layoutResourceId)
			return false;
		return true;
	}



	public int getImageWidth() {
		return imageWidth;
	}

	public void setImageWidth(int imageWidth) {
		this.imageWidth = imageWidth;
	}

	private class ViewAdapter {
		public TextView tvName;
		public TextView tvSize;
		public TextView tvDate;
		public RecyclingImageView rivImage;
	}

	public void inits() {
		aq = new AQuery(context);
		placehoder = aq.getCachedImage(layoutResourceId);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// lay layout tuong ung cua moi row
		// set gia tri cho cac tp cua rung row
		if (convertView == null) {
			viewAdapter = new ViewAdapter();
			convertView = inflater.inflate(R.layout.row_grid, parent, false);
			viewAdapter.tvName = (TextView) convertView
					.findViewById(R.id.tvName);
			viewAdapter.tvSize = (TextView) convertView
					.findViewById(R.id.tvSize);
			viewAdapter.tvDate = (TextView) convertView
					.findViewById(R.id.tvDate);
			viewAdapter.rivImage = (RecyclingImageView) convertView
					.findViewById(R.id.rivImage);
			convertView.setTag(viewAdapter);
		} else {
			viewAdapter = (ViewAdapter) convertView.getTag();
		}

		if (imageItems.get(position).getName().length() <= 20) {
			viewAdapter.tvName.setText(imageItems.get(position).getName());
		} else {
			viewAdapter.tvName
					.setText(imageItems.get(position).getName().substring(0, 5)
							+ "..."
							+ imageItems
									.get(position)
									.getName()
									.substring(
											imageItems.get(position).getName()
													.length() - 3));
		}
		viewAdapter.tvSize.setText(imageItems.get(position).getSize());
		viewAdapter.tvDate.setText(imageItems.get(position).getDate());
		 
		//quan trong 
		viewAdapter.rivImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
//		viewAdapter.rivImage.setLayoutParams(new GridView.LayoutParams(imageWidth,imageWidth));
		if (imageItems.get(position).getImageUrl().equals("")) {
			// mo anh tu album
			// Bitmap bmp = BitmapFactory.decodeFile(imageItems.get(position)
			// .getImagePath());
			// fix bug: out of memory khi load image
			Bitmap bmp = imageItems.get(position).decodeSampleBitmapFromUri(
					imageItems.get(position).getImagePath(), imageWidth,
					imageWidth);
			viewAdapter.rivImage.setImageBitmap(bmp);
			// bmp.recycle();
		} else {
			// load anh tu url
			getImageFromUrl(imageItems.get(position).getImageUrl(),
					viewAdapter.rivImage);
		}
		// aq.id(rivImage).image(imageItems.get(position).getImageUrl(), true,
		// true, 0, 0, placehoder, AQuery.FADE_IN_NETWORK, 1.0f);

		return convertView;
	}

	public void getImageFromUrl(String url, RecyclingImageView riv) {
		aq.id(riv).image(url, true, true, 0, 0, placehoder,
				AQuery.FADE_IN_NETWORK, 1.0f);

	}

}
